local LUI = LibStub("AceAddon-3.0"):GetAddon("LUI")
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists
local LUIHook = LUI:GetModule("LUIHook")
local module = LUI:NewModule("ExpBar", "AceHook-3.0")
local db;

local fontflags = {'OUTLINE', 'THICKOUTLINE', 'MONOCHROME', 'NONE'}
local chatAlignments = {'LEFT', 'CENTER', 'RIGHT'}

local defaults = {
	ExpBar = {
		Enable = true,
		showPercent = true,
		alwaysShow = true,
		PercentFont = "Arial Narrow",
		PercentFontSize = 14,
		PercentFontFlag = "NONE",
		PercentFontJustify = "CENTER",
		PercentFontColors = {255, 255, 255, 1},
	},
}
local function SetPercentFont()
	if (module.frame.text) then
		module.frame.text:SetFont(LSM:Fetch("font", db.ExpBar.PercentFont), db.ExpBar.PercentFontSize, db.ExpBar.PercentFontFlag)
	end
end
local function SetPercentJustify()
	if (module.frame.text) then
		module.frame.text:SetJustifyH(db.ExpBar.PercentFontJustify);
	end
end
local function SetPercentColor(...)
	local r = db.ExpBar.PercentFontColors[0] or 0;
	local g = db.ExpBar.PercentFontColors[1] or 0;
	local b = db.ExpBar.PercentFontColors[2] or 0;
	local a = db.ExpBar.PercentFontColors[3] or 0;
	
	if (module.frame.text) then
		module.frame.text:SetTextColor(r, g, b, a);
	end
end
function module:LoadOptions()
	local options = {
		ExpBar = {
			name = "ExpBar",
			type = "group",
			order = 50,
			disabled = function() return not db.ExpBar.Enable or not IsAddOnLoaded("oUF_Experience") end,
			childGroups = "select",
			args = {
				ShowPercent = {
					name = "Show Percent",
					desc = "Show's a textual representation of the experience percent into level you are.",
					disabled = function() return not db.ExpBar.Enable end,
					type = "toggle",
					width = "full",
					get = function() return db.ExpBar.showPercent end,
					set = function(self, ShowPer)
						db.ExpBar.showPercent = not db.ExpBar.showPercent;
						module:HandleUpdate("percent");
					end,
					order = 1,
				},
				AlwaysShow = {
					name = "Keep bar visible always.",
					desc = "Keeps the bar visible at all times.",
					disabled = function() return not db.ExpBar.Enable end,
					type = "toggle",
					width = "full",
					get = function() return db.ExpBar.alwaysShow end,
					set = function(self, Sho)
						db.ExpBar.alwaysShow = not db.ExpBar.alwaysShow;
						module:HandleUpdate("visible");
					end,
					order = 2
				},
				PercentFont = {
					name = "PercentFont",
					desc = "Choose your font!\nDefault: "..LUI.defaults.profile.ExpBar.PercentFont,
					type = "select",
					disabled = function() return not db.ExpBar.showPercent; end,
					dialogControl = "LSM30_Font",
					values = widgetLists.font,
					get = function() return db.ExpBar.PercentFont end,
					set = function(self, font)
						db.ExpBar.PercentFont = font;
						SetPercentFont();
					end,
					order = 3,
				},
				PercentFontSize = {
					name = "PercentFontSize",
					desc = "Choose your font size!\nDefault: "..LUI.defaults.profile.ExpBar.PercentFontSize,
					type = "range",
					disabled = function() return not db.ExpBar.showPercent; end,
					min = 6,
					max = 20,
					step = 1,
					get = function() return db.ExpBar.PercentFontSize; end,
					set = function(self, font)
						db.ExpBar.PercentFontSize = font;
						SetPercentFont();
					end,
					order = 4,
				},
				PercentFontFlag = {
					name = "PercentFontFlag",
					desc = "Choose your font flag!\nDefault: "..LUI.defaults.profile.ExpBar.PercentFontFlag,
					disabled = function() return not db.ExpBar.showPercent; end,
					type = "select",
					values = fontflags,
					get = function()
						for k,v in pairs(fontflags) do
							if (db.ExpBar.PercentFontFlag == v) then
								return k;
							end
						end
						print("Failed to find flag! '" .. db.ExpBar.PercentFontFlag .. "'.");
					end,
					set = function(self, FontFlag)
						print("Setting FontFlag to '"..FontFlag.."'.");
						db.ExpBar.PercentFontFlag = fontflags[FontFlag];
						SetPercentFont();
					end,
					order = 5,
				},
				PercentFontColor = {
					name = "Choose font color",
					desc = "Choose the color for the percent font.",
					disabled = function() return not db.ExpBar.showPercent; end,
					type = "color",
					width = "full",
					hasAlpha = false,
					get = function() return unpack(db.ExpBar.PercentFontColors); end,
					set = function(_, r, g, b, a)
						db.ExpBar.PercentFontColors = {r,g,b,a}
						SetPercentColor();
					end,
					order = 6,
				},
				PercentFontJustify = {
					name = "Choose Alignment",
					desc = "Choose the Alignment for your percentage.\nDefault: "..LUI.defaults.profile.ExpBar.PercentFontJustify,
					disabled = function() return not db.ExpBar.showPercent; end,
					type = "select",
					values = chatAlignments,
					get = function()
						for k, v in pairs(chatAlignments) do
							if db.ExpBar.PercentFontJustify == v then
								return k
							end
						end
					end,
					set = function(self, ChatJustify)
						db.ExpBar.PercentFontJustify = chatAlignments[ChatJustify]
						SetPercentJustify()
					end,
					order = 6,
				},
			},
		},
	}
	return options
end
function module:HandleUpdate(t)
	if (t == "percent") then
		if (db.ExpBar.showPercent) then
			if (not module.frame.text) then
				module:CreatePercent();
			end
		else
			if (module.frame.text) then
				module.frame.text:Hide();
				module.frame.text = nil;
			end
		end
	elseif (t == "visible") then
		if (db.ExpBar.alwaysShow) then
			module:SetupVisible();
		else
			module:DisableVisible();
		end
	end
end
function module:LoadModule() 
	local module = {
		ExpBar = {
			order = LUI:GetModuleCount(),
			type = "execute",
			name = function()
				if db.ExpBar.Enable then
					return "|cff00ff00ExpBar Enabled|r"
				else
					return "|cffFF0000ExpBar Disabled|r"
				end
			end,
			func = function(self, ExpBar)
				db.ExpBar.Enable = not db.ExpBar.Enable
				StaticPopup_Show("RELOAD_UI");
			end
		},
	}
	return module;
end
function module:OnInitialize()
	LUI:MergeDefaults(LUI.db.defaults.profile, defaults)
	LUI:RefreshDefaults()
	LUI:Refresh()
	
	self.db = LUI.db
	db = self.db.profile
end

local function Update(self, unit, mmin, mmax)
	if not db.ExpBar.showPercent then
		return;
	end
	
	if (IsAddOnLoaded("oUF_Experience")) then
		if (unit == "player") then
			if (module.frame.text) then
				local prct = math.floor(((mmin/mmax)*100)+.5);
				module.frame.text:SetText(prct.."%");
			end
		end
	end
end

function module:CreatePercent()
	module.frame.text = module.frame:CreateFontString(nil, "OVERLAY");
	module.frame.text:SetFont(LSM:Fetch("font", db.ExpBar.PercentFont), tonumber(db.ExpBar.PercentFontSize), db.ExpBar.PercentFontFlag);
	module.frame.text:SetJustifyH(db.ExpBar.PercentFontJustify);
	module.frame.text:SetShadowColor(0, 0, 0);
	module.frame.text:SetShadowOffset(1.25, -1.25);
	module.frame.text:SetWidth(module.frame:GetWidth());
	module.frame.text:SetPoint("CENTER", module.frame, "CENTER", 0, 0);
	mmin, mmax = UnitXP("player"), UnitXPMax("player");
	local prct = math.floor(((mmin/mmax)*100)+.5);
	module.frame.text:SetText(prct.."%");
	module.frame.text:Show();
	module.frame.PostUpdate = Update;
	SetPercentColor();
	SetPercentFont();
	SetPercentJustify();
end
function module:DisableVisible()
	module.frame:SetAlpha(0);
	module.frame:SetScript("OnEnter", function() module.frame:SetAlpha(1); end);
	module.frame:SetScript("OnLeave", function() module.frame:SetAlpha(0); end);
end
function module:SetupVisible()
	module.frame:SetAlpha(1);
	module.frame:SetScript("OnEnter", nil);
	module.frame:SetScript("OnLeave", nil);
end
function module:OnEnable()
	LUI:RegisterOptions(module:LoadOptions());
	LUI:RegisterModule(module:LoadModule());
	print("ExpBar : OnEnable");
	if IsAddOnLoaded("oUF_Experience") then
		module.frame = _G["oUF_LUI_player_Experience"];
		if (not module.frame) then
			db.ExpBar.Enable = false;
			return;
		end
		if (db.ExpBar.Enable) then
			if (db.ExpBar.showPercent) then
				module:CreatePercent();
			end
			if (db.ExpBar.alwaysShow) then
				module:SetupVisible();
			end
		end
	else
	end
end